const db = wx.cloud.database();
const app = getApp();
Page({
  data: {
    nianji: 0,
    nianjis: ['年级','大一', '大二', '大三', '大四', '大五','研一', '研二', '研三', '博士'],
    index: 0,
    index2: 0,
    index3: 0,
    index4: 0,
    seletid:0,
    seletid2:0,
    school: ['学校'],
    xueyuan: ['学院'],
    xiangmu: ['备考项目'],
    leibie: ['具体类别'],
    phone: 0,
    success:''
  },
  nianjiChange: function (e) {
    console.log('nianji发送选择改变，携带值为', e.detail.value)
    this.setData({
      nianji: e.detail.value
    })
  },
  nameInput: function (e) {
    this.setData({
      name: e.detail.value
    })
    console.log(this.data.name)
  },
  zhuanyeInput: function (e) {
    this.setData({
      zhuanye: e.detail.value
    })
    console.log(this.data.zhuanye)
  },
  beizhuInput: function (e) {
    this.setData({
      beizhu: e.detail.value
    })
    console.log(this.data.beizhu)
  },

  bindPickerChange: function (e) {
    let num = e.detail.value;
    let that = this;
    console.log('school发送选择改变，携带值为', that.data.school[num], that.data.seletid[num]);
    this.setData({
        index: e.detail.value
    })

    wx.request({ //发送请求
        url: 'https://zg99.offcn.com/index/chaxun/getcascade?actid=41521&callback=?', //接收url
        data: {
            level: '2',
            tabfzid: that.data.seletid[num]
        }, //接收参数
        success(res) {
            var jsonStr = res.data;
            jsonStr = jsonStr.replace("(", "");
            jsonStr = jsonStr.replace(")", "");
            if (typeof jsonStr != 'object') {
                jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
                var theJson = JSON.parse(jsonStr);
            }
            var list = [];
            for (var i in theJson.lists) { //循环写入下来框
                list.push(theJson.lists[i].xueyuan)
            }
            that.setData({
                xueyuan: list
            });
        }
    })
},
bindPickerChange2: function (e) {
    let num2 = e.detail.value;
    let that = this;
    console.log('xueyuan发送选择改变，携带值为', that.data.xueyuan[num2]);
    this.setData({
        index2: e.detail.value
    })
},
bindPickerChange3: function (e) {
  let num3 = e.detail.value;
  let that = this;
  console.log('xiangmu发送选择改变，携带值为', that.data.xiangmu[num3], that.data.seletid2[num3]);
  this.setData({
      index3: e.detail.value
  })
  wx.request({ //发送请求
      url: 'https://zg99.offcn.com/index/chaxun/getcascade?actid=41521&callback=?', //接收url
      data: {
          level: '2',
          tabnum:'2',
          tabfzid: that.data.seletid2[num3]
      }, //接收参数
      success(res) {
          var jsonStr = res.data;
          jsonStr = jsonStr.replace("(", "");
          jsonStr = jsonStr.replace(")", "");
          if (typeof jsonStr != 'object') {
              jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
              var theJson = JSON.parse(jsonStr);
          }
          var list = [];
          for (var i in theJson.lists) { //循环写入下来框
              list.push(theJson.lists[i].leibie)
          }
          that.setData({
              leibie: list
          });
      }
  })
},
bindPickerChange4: function (e) {
  let num4 = e.detail.value;
  let that = this;
  console.log('xueyuan发送选择改变，携带值为', that.data.leibie[num4]);
  this.setData({
      index4: e.detail.value
  })
},












  onLoad: function (options) {
    var that = this;
    console.log(options);
    that.setData({
      area: options.area,
      geneal: options.agent
    })
    
    wx.request({ //发送请求
      url: 'https://zg99.offcn.com/index/chaxun/getcascade?actid=41521&callback=?', //接收url
      data: {
          level: '1',
          tabnum:'1'
      }, //接收参数
      success(res) { //返回
          var jsonStr = res.data;
          jsonStr = jsonStr.replace("(", "");
          jsonStr = jsonStr.replace(")", "");
          if (typeof jsonStr != 'object') {
              jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
              var theJson = JSON.parse(jsonStr);
          }
          var list = [];
          var listid = [];
          for (var i in theJson.lists) { //循环写入下来框
              list.push(theJson.lists[i].school)
              listid.push(theJson.lists[i].id)
          }
          that.setData({
              school: list,
              seletid: listid
          });
      }
  })
  
  wx.request({ //发送请求
    url: 'https://zg99.offcn.com/index/chaxun/getcascade?actid=41521&callback=?', //接收url
    data: {
        level: '1',
        tabnum:'2'
    }, //接收参数
    success(res) { //返回
        var jsonStr = res.data;
        jsonStr = jsonStr.replace("(", "");
        jsonStr = jsonStr.replace(")", "");
        if (typeof jsonStr != 'object') {
            jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
            var theJson = JSON.parse(jsonStr);
        }
        var list = [];
        var listid = [];
        for (var i in theJson.lists) { //循环写入下来框
            list.push(theJson.lists[i].xiangmu)
            listid.push(theJson.lists[i].id)
        }
        that.setData({
            xiangmu: list,
            seletid2: listid
        });
    }
})
    
  },
  getPhoneNumber(e) {
    db.collection('userInfo').where({
      _openid: app.globalData.openid,
    }).get({
      success: res => {
        console.log(res.data.length)
        if (res.data.length == 0) {
          var that = this;
          wx.cloud.callFunction({
            name: 'getMobile',
            data: {
              weRunData: wx.cloud.CloudID(e.detail.cloudID),
            }
          }).then(res => {
            that.setData({
              mobile: res.result,
            })
            console.log(res.result)

            if (that.data.name && that.data.nianji&& that.data.xueyuan&& that.data.leibie&& that.data.zhuanye) {
              wx.request({ //发送请求
              url: "https://zg99.offcn.com/index/chaxun/register/?actid=41521&callback=?",
              data: {
                  phone: res.result,
                  fenxiao: that.data.area,
                  geneal: that.data.geneal,
                  name: that.data.name,
                  nianji: that.data.nianjis[that.data.nianji],
                  school: that.data.school[that.data.index],
                  xueyuan: that.data.xueyuan[that.data.index2],
                  xiangmu: that.data.xiangmu[that.data.index3],
                  leibie: that.data.leibie[that.data.index4],
                  zhuanye: that.data.zhuanye,
                  beizhu: that.data.beizhu
              }, //接收参数
              success(res) { //返回
                  var jsonStr = res.data;
                  jsonStr = jsonStr.replace("(", "");
                  jsonStr = jsonStr.replace(")", "");
                  if (typeof jsonStr != 'object') {
                      jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
                      var theJson = JSON.parse(jsonStr);
                  };
                  console.log(theJson)
                  that.setData({
                    success:'success',
                  })
                  
              }
                })
               wx.request({
            url: 'https://tgadmin.sdwx.zgkfpt.cn/api/v1/collect_v3',
            method: 'POST',
            data: {
              activeId: 30,
              mobile: res.result,
              areaCode: that.data.area,
              agent: that.data.geneal,
              pageId: that.data.page
            },
            success: res => {
              console.log(res);
            }
                })
              } else {
                  wx.showToast({
                    title: '请完整填写信息后领取',
                    duration: 2000,
                    icon: 'none'
                  })
                }
          }).catch(err => {
            console.error(err);
          });

        } else {
          wx.showToast({
            title: '请完整填写信息',
            duration: 2000,
            icon: 'none'
          })
        }
      },
    })
  }


})


